function regionalGMAnalysisMain(inDir,filter,outDir,stat,varName)

%filter='*alpha*';
files=dir(fullfile(inDir,filter));
nAreas=16;
fileNames=cell(length(files),1);
areas='';
areaLabels='';
mAnova=[];
for i=1:length(files)
    fileNames{i}=files(i).name;
    info=whos('-file',fullfile(inDir,fileNames{i}));    
    varName=info.name;
    load(fullfile(inDir,fileNames{i}),varName);
    nSubjs=size(eval(varName),1);
    regional.intraArea=zeros(nSubjs,nAreas)
    regional.interArea=zeros(nSubjs,(nAreas*(nAreas-1))/2);
     for j=1:nSubjs
        m=squeeze(eval(strcat(varName,'(j,:,:)')));
        [areas,areaLabels,regional.intraArea(j,:),regional.interArea(j,:)]=regionalAnalysis(m);
     end
     [cond,rem]=strtok(fileNames{i},'.');
     save(fullfile(outDir,strcat(cond,'_regional')),'regional','areas','areaLabels');   
     mAnova=[mAnova regional.intraArea];
end
[band,rem]=strtok(fileNames{1},'_');
save(fullfile(outDir,strcat(band,'_names')),'fileNames');   
fileout=strcat(band,'_rmAnova.txt');
levels=[4 2 8];
varNames={'Condition','Hemisphere','Area'};

diary(fullfile(outDir,fileout))
O = teg_repeated_measures_ANOVA(mAnova, levels, varNames);
diary off;






[areas,areaLabels]=areasAndLabels();
[intraArea]=short(m,areas);
[interArea]=long(m,areas);

function [areas,arealabels]=areasAndLabels()

[arealabels]=getAreaLabels();
[areas]=getAreas();

function [arealabels]=getAreaLabels()
arealabels={'laf';'lf';'lfc';'lc';'lcp';'lp';'lpo';'lt';'raf';'rf';'rfc';'rc';'rcp';'rp';'rpo';'rt'};
function [areas]=getAreas()

laf=[22 25 26];
lf=[30 31 32 33];
lfc=[1 2 3];
lc=[8 9 10];
lcp=[15 16 17];
lp=[47 48 49 50];
lpo=[56 57 61];
lt=[39 41 43 45];

raf=[24 28 29];
rf=[35 36 37 38];
rfc=[5 6 7];
rc=[12 13 14];
rcp=[19 20 21];
rp=[52 53 54 55];
rpo=[50 60 63];
rt=[40 42 44 46];

areas={laf;lf;lfc;lc;lcp;lp;lpo;lt;raf;rf;rfc;rc;rcp;rp;rpo;rt};

function [v]=short(V,areas)

v=zeros(1,length(areas));
for i=1:length(areas)
    m=V(areas{i});     
     v(i)=mean(m);
end


